#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

use rocket::serde::json::Json;
use crate::db::{establish_connection, queries::get_users};
use crate::models::User;
use actix_web::{ web, App, HttpServer };
use diesel::r2d2::{ self, ConnectionManager };
use diesel::PgConnection;
use std::env;

mod api;
mod db;
mod models;
mod schema;

#[get("/users")]
async fn get_users() -> Json<Vec<User>> {
    let pool = establish_connection().await;
    let users = get_users(&pool).await;
    Json(users)
}

#[rocket::main]
async fn main() {
    rocket::ignite()
        .mount("/", routes![get_users])
        .launch()
        .await
        .expect("Failed to launch Rocket");
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv::dotenv().ok();
    env_logger::init();

    let pool = db::establish_connection();

    HttpServer::new( || {
        App::new()
            .data(pool.clone())
            .service(web::scope("/api").configure(api::config))
            .service(
                web::scope("/tasks"
                .route("", web::post().to(create::create_task))
                .route("/{id}", web::put().to(update::update_task))
                .route("/{id}", web::delete().to(delete::delete_task))
                .route("", web::get().to(list::list_tasks))
                )
            )
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}


